﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using NetJsWire.Lite.Widgets.UI;
using Panel = NetJsWire.Lite.Widgets.UI.Panel;

namespace NetJsWire.Lite.Designers
{
    public sealed class TabPanelDesigner : ContainerDesigner
    {
        private TabPanelLW _panel;
        public override void Initialize( IComponent component )
        {
            base.Initialize( component );
            _panel = Control as TabPanelLW;
        }

        protected override void WndProc( ref Message m )
        {
            switch ( ( Native.Msg ) m.Msg )
            {
                case Native.Msg.WM_NCMBUTTONDBLCLK:
                case Native.Msg.WM_NCMBUTTONDOWN:
                case Native.Msg.WM_NCRBUTTONDBLCLK:
                case Native.Msg.WM_NCLBUTTONDBLCLK:
                case Native.Msg.WM_NCLBUTTONDOWN:
                case Native.Msg.WM_NCLBUTTONUP:
                    var lParam = m.LParam.ToInt32();
                    var x = ( lParam << 16 ) >> 16; // lo order word
                    var y = lParam >> 16; // hi order word
                    var screenPoint = new Point( x, y );
                    var pt = _panel.PointToClient( screenPoint );
                    if ( !_panel.ClientRectangle.Contains( pt ) )
                    {
                        var realPoint = new Point( pt.X, Panel.TitleBarHeight + pt.Y );
                        var comp = _panel.GetComponentAtPoint( realPoint );
                        if ( comp != null )
                        {
                            _panel.ActiveItem = _panel.Controls.IndexOf( comp );
                            if ( SelectionServ != null )
                            {
                                SelectionServ.SetSelectedComponents( new object[] { comp } );
                            }
                            m.Result = IntPtr.Zero;
                            return;
                        }
                    }

                    base.WndProc( ref m );
                    break;
                default:
                    base.WndProc( ref m );
                    break;
            }
        }
    }
}
